AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
util.PrecacheSound( "Airboat_engine_idle" )
util.PrecacheSound( "Airboat_engine_stop" )
util.PrecacheSound( "apc_engine_start" )

include('shared.lua')

local Ground = 1 + 0 + 2 + 8 + 32
local EnerCost = 50
local ResGen = 15


function ENT:Initialize()
	self.Entity:PhysicsInit( SOLID_VPHYSICS )
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
	self.Entity:SetSolid( SOLID_VPHYSICS )
	
	local phys = self.Entity:GetPhysicsObject()
	self.val1 = 0
	self.GenAir = 0
	self.GenCool = 0
	self.GenHydro = 0
	self.lastused = 0
	self.damaged = 0
	self.heat = 0

	if not (WireAddon == nil) then self.Inputs = Wire_CreateInputs(self.Entity, { "On", "Air", "Coolant", "Hydrogen" }) end
	
	self.timer = CurTime() +  1
	if (phys:IsValid()) then
		phys:Wake()
		phys:SetMass(1000)

	end
end

function ENT:Setup()
	self:TriggerInput("On", 0)
	self:TriggerInput("Air", 0)
	self:TriggerInput("Coolant", 0)
	self:TriggerInput("Hydrogen", 0)
end

function ENT:TriggerInput(iname, value)
	if (iname == "On") then
		if (value ~= 0) then
			if ( self.Active == 0 ) then
				self.Active = 1
			end
		else
			if ( self.Active == 1 ) then
				self.Active = 0
			end
		end
	end
	if (iname == "Air") then
		if (value ~= 0) then
			if ( self.GenAir == 0 ) then
				self.GenAir = 1
			end
		else
			if ( self.GenAir == 1 ) then
				self.GenAir = 0
			end
		end
	end
	if (iname == "Coolant") then
		if (value ~= 0) then
			if ( self.GenCool == 0 ) then
				self.GenCool = 1
			end
		else
			if ( self.GenCool == 1 ) then
				self.GenCool = 0
			end
		end
	end
	if (iname == "Hydrogen") then
		if (value ~= 0) then
			if ( self.GenHydro == 0 ) then
				self.GenHydro = 1
			end
		else
			if ( self.GenHydro == 1 ) then
				self.GenHydro = 0
			end
		end
	end
end

function ENT:Damage()
	if (self.damaged == 0) then
		self.damaged = 1
	end
	if ((self.Active == 1) and (math.random(1, 10) <= 4)) then
		self.Active = 0
	end
end

function ENT:Repair()
	self.Entity:SetColor(255, 255, 255, 255)
	self.health = self.max_health
	self.damaged = 0
end

function ENT:Destruct()
	local Effect = EffectData()
		Effect:SetOrigin(self.Entity:GetPos())
		Effect:SetScale(1)
		Effect:SetMagnitude(25)
		util.Effect("Explosion", Effect, true, true)

		self.Entity:Remove()
end

function ENT:OnRemove()
	Dev_Unlink_All(self.Entity)
end


function ENT:Use()
end

function ENT:Output()
	return 1
end

function ENT:Gen_Res()
	local MultFac = 0
	if (self.GenAir == 1) then MultFac = MultFac+1 end
	if (self.GenCool == 1)then MultFac = MultFac+1 end
	if (self.GenHydro == 1) then MultFac = MultFac+1 end
	local fuel = RD_GetResourceAmount(self, "energy")
	
	if (fuel >= EnerCost*MultFac) then
		if (self.GenAir == 1) then
			RD_SupplyResource(self, "air", ResGen)
		end
		if (self.GenCool == 1) then
			RD_SupplyResource(self, "coolant", ResGen)
		end
		if (self.GenHydro == 1) then
			RD_SupplyResource(self, "hydrogen", ResGen)
		end
		RD_ConsumeResource(self, "energy", EnerCost*MultFac)
	else
		self.Active = 0
	end
	return
end

function ENT:Think()
	if ( self.Active == 0 ) then
		self:SetOverlayText( "Universal Resource Generator\n(OFF)\nEnergy: " .. self.val1 )
	else
		self:SetOverlayText( "Universal Resource Generator\n(ON)\nEnergy: " .. self.val1 )
	end
	if ( self.timer > CurTime() ) then return end
	self.val1 = RD_GetResourceAmount(self, "energy")
	if ( self.Active == 1 ) then self:Gen_Res() end
	self.timer = CurTime() +  1
end


function ENT:AcceptInput(name,activator,caller)
	if name == "Use" and caller:IsPlayer() and caller:KeyDownLast(IN_USE) == false then
		if ( self.Active == 0 ) then
			self.Active = 1
			self.lastused = CurTime()
		else
		self.Active = 0
		end
	end
end

function ENT:PreEntityCopy()
	RD_BuildDupeInfo(self.Entity)
	if (WireAddon == 1) then
		local DupeInfo = Wire_BuildDupeInfo(self.Entity)
		if DupeInfo then
			duplicator.StoreEntityModifier( self.Entity, "WireDupeInfo", DupeInfo )
		end
	end
end

function ENT:PostEntityPaste( Player, Ent, CreatedEntities )
	RD_ApplyDupeInfo(Ent, CreatedEntities)
	if (WireAddon == 1) then
		if (Ent.EntityMods) and (Ent.EntityMods.WireDupeInfo) then
			Wire_ApplyDupeInfo(Player, Ent, Ent.EntityMods.WireDupeInfo, function(id) return CreatedEntities[id] end)
		end
	end
end
